<?php

namespace app\admin\controller;

use app\admin\model\Role;
use app\admin\model\Staff;
use app\admin\model\Store;
use app\admin\model\Store as modelStore;
use app\BaseController;
use app\Request;
use Tool\CryptAES;

class Department extends BaseController
{

    public function roleList()
    {


        if (\request()->isAjax() && \request()->isGet()) {

            $result = Role::getList();;

            return $this->success($result);
        }


        return view();
    }


    public function editRole(Request $request)
    {


        $id = input('id');
        if ($request->isPost() && $request->isAjax()) {

            $id = input('id');

            empty($id) ? $type = 'add' : $type = 'edit';
            if ($type == 'add') {
                $result = Role::insertRole(input(''));
                return json($result);
            } else {

                $result = Role::modifyRole(input(''));
                return json($result);
            }
        }

        $info = Role::where(['id' => $id])->find();
        $info ? $info = $info->getData() : $info = [];


        empty($id) ? $tpl = 'add_role' : $tpl = 'edit_role';


        $stores = modelStore::select();

        return \view($tpl, ['info' => $info, 'stores' => $stores]);
    }

    public function RoleDel()
    {


        (new Role())->where(['id' => input('id')])->delete();
        return $this->success();

    }


    public function staffList()
    {


        if (\request()->isAjax() && \request()->isGet()) {

            $result = Staff::getList();;

            foreach ($result as $key => $value) {
                $store = new Store();
                $stores = $store->column('name', 'id');
                $value->store_name = $stores[$value->store_id];
            }
            return $this->success($result);
        }


        return view();
    }

    public function editStaff(Request $request)
    {

        $id = input('id');
        if ($request->isPost() && $request->isAjax()) {

            $data = input('');
            unset($data['thumbs']);
            $id = input('id');

            empty($id) ? $type = 'add' : $type = 'edit';
            if ($type == 'add') {
                $result = Staff::insertStaff($data);
                return json($result);
            } else {

                $result = Staff::modifyStaff($data);
                return json($result);
            }
        }

        $info = Staff::where(['id' => $id])->find();
        $info ? $info = $info->getData() : $info = [];


        empty($id) ? $tpl = 'add_staff' : $tpl = 'edit_staff';


        $stores = modelStore::select();

        $role = new Role();

        $roles = $role->getList();
        return \view($tpl, ['info' => $info, 'stores' => $stores, 'roles' => $roles]);
    }

    public function adminList()
    {


        if (\request()->isAjax() && \request()->isGet()) {

            $result = \app\admin\model\Admin::getList();;

            return $this->success($result);
        }


        return view();
    }

    public function editAdmin(Request $request)
    {

        $id = input('id');
        if ($request->isPost() && $request->isAjax()) {

            $data = input('');
            unset($data['thumbs']);
            $id = input('id');

            empty($id) ? $type = 'add' : $type = 'edit';
            if ($type == 'add') {
                $result = \app\admin\model\Admin::insertAdmin($data);
                return $this->success($result);
            } else {

                $result = \app\admin\model\Admin::modifyAdmin($data);
                return $this->success($result);
            }
        }

        $info = \app\admin\model\Admin::where(['id' => $id])->find();
        $info ? $info = $info->getData() : $info = [];


        $info['password']=CryptAES::aesDecode($info['password']);

        empty($id) ? $tpl = 'add_admin' : $tpl = 'edit_admin';


        $stores = modelStore::select();

        $role = new Role();

        $roles = $role->getList();
        return \view($tpl, ['info' => $info, 'stores' => $stores, 'roles' => $roles]);
    }


}